package com.csc531.settingsPanel;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.EditText;

import com.csc531.R;
import com.csc531.adapters.ManagerDbAdapter;

public class Settings extends Activity
{

	//logging
	private static final String TAG = "settings";

	//database 
	private Long _rowId;
	private ManagerDbAdapter _mDbHelper;

	private EditText _email;
	private EditText _password;
	private CheckBox _enabled;	

	//menu
	static final int SAVE = Menu.FIRST;

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		super.onCreateOptionsMenu(menu);
		menu.add(0, SAVE, 0, R.string.task_edit_menu_item1);

		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
		switch(item.getItemId()) 
		{
		case SAVE:
			exitActivity();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	private void exitActivity()
	{
		setResult(RESULT_OK);
		finish();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);

		initializeDatabaseAdapter();
		initializeSettingFields(savedInstanceState);
		populateSettingFields();
	}

	private void initializeDatabaseAdapter()
	{
		_mDbHelper = new ManagerDbAdapter(this);
		_mDbHelper.open();

	}

	private void initializeSettingFields(Bundle savedInstanceState)
	{
		_email = (EditText) findViewById(R.id.gemail_field);
		_password = (EditText) findViewById(R.id.gpassword_field);
		_enabled = (CheckBox) findViewById(R.id.check_for_addevent);

		_rowId = savedInstanceState != null ? savedInstanceState.getLong(ManagerDbAdapter.KEY_ROWID) : null;
	}


	private void populateSettingFields() 
	{
		try
		{
			Cursor calendarSettings = _mDbHelper.FetchEmailAndPasswordAndEnable();
			startManagingCursor(calendarSettings);

			if (calendarSettings.moveToFirst() == true) 
			{
				String enabled = "";

				_email.setText(calendarSettings.getString(
						calendarSettings.getColumnIndexOrThrow(ManagerDbAdapter.EMAIL)));
				_password.setText(calendarSettings.getString(
						calendarSettings.getColumnIndexOrThrow(ManagerDbAdapter.PASSWORD)));
				enabled = calendarSettings.getString(
						calendarSettings.getColumnIndexOrThrow(ManagerDbAdapter.ENABLE_GOOGLE));

				if(enabled.compareTo("true") == 0)
				{
					_enabled.setChecked(true);
				}
				else
				{
					_enabled.setChecked(false);
				}
			}
		}
		catch(Exception ex) {

		}
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() 
	{
		super.onPause();
		saveSettings();
		closeConnection();
	}

	private void saveSettings() 
	{
		String email = _email.getText().toString();
		String password = _password.getText().toString();
		String enabled = "";

		if(_enabled.isChecked())
		{
			enabled = "true";
		}
		else
		{
			enabled = "false";
		}

		if(email.length() == 0 || password.length() == 0) 
		{
			Log.v(TAG,"Nothing in here returning nothing");
			return;
		}

		try{
			_mDbHelper.FetchEmailAndPasswordAndEnable(email);
			boolean result = _mDbHelper.updateGoogleCredentials(email, password, enabled);
			if(result) {
				Log.v(TAG,"UPDATED email: "+email);
			}
		}catch(Exception ex) {
			Log.v(TAG,"SAVING email: "+email);
			_mDbHelper.AddGoogleCredentials(email, password, enabled);
		}

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) 
	{
		super.onSaveInstanceState(outState);
		closeConnection();
	}

	private void closeConnection()
	{
		_mDbHelper.close();
	}

	public Long getRowId() {
		return _rowId;
	}
}
